#!/bin/bash

# Slackware build script for xli

# Copyright 2014 Leonard Schmidt <email removed>
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright
# notice and this permission notice appear in all copies.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Now maintained by B. Watson <urchlay@slackware.uk>

# 20180629 bkw:
# - Take over maintenance.
# - BUILD=2.
# - Stop spamming /etc/X11/app-defaults and /usr/bin (!!)
# - Add -DHAVE_GUNZIP and -DHAVE_BUNZIP2 options, to support viewing
#   compressed images.
# - Install xliguide.5 man page.
# - Make system-wide config file actually work as documented in the
#   man page. Previous it was getting installed (straight to /etc, not
#   as part of the package!) and totally ignored at runtime.
# - Include chkgamma.jpg in doc dir, since ABOUTGAMMA references it.

# TODO: find out why "-border" doesn't seem to work. The -fullscreen
# window border is always white, no matter what color you give to
# -border. I only moderately care about this, TBH.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=xli
VERSION=${VERSION:-1.17}
SRCVER=${SRCVER:-2005-02-27}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/$PRGNAM-$SRCVER.tar.gz
cd $PRGNAM-$SRCVER
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# These patches are from NetBSD's pkgsrc:
# <http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/graphics/xli/patches/?only_with_tag=MAIN>
for i in c d e f g h; do
	patch -p0 < $CWD/patch-a${i}
done

# 20180629 bkw: Seems we have to explicitly define the system-wide
# config file in CCOPTIONS. Might as well add .gz and .bz2 support,
# while we're at it.
APPDEF=/etc/X11/app-defaults/Xli
xmkmf -a
make \
  CDEBUGFLAGS="$SLKCFLAGS" \
  CCOPTIONS="-DHAVE_GUNZIP -DHAVE_BUNZIP2 -DSYSPATHFILE=\\\"$APPDEF\\\""

# 20180629 bkw: Do not use 'make install', it doesn't fully support
# DESTDIR and spams stuff to /usr/bin and /etc/X11/app-defaults.
mkdir -p $PKG/usr/bin $PKG/usr/man/man1 $PKG

for i in $PRGNAM ${PRGNAM}to; do
  install -s -m0755 -oroot -groot $i $PKG/usr/bin
  gzip -9c < $PRGNAM.man > $PKG/usr/man/man1/$i.1x.gz
done

for i in xview xsetbg; do
  ln -s $PRGNAM $PKG/usr/bin/$i
  ln -s $PRGNAM.1x.gz $PKG/usr/man/man1/$i.1x.gz
done

# This is API doc, for extending xli with new image formats. Not
# necessarily useful, but why not package it?
mkdir -p $PKG/usr/man/man5
gzip -9c < xliguide.man > $PKG/usr/man/man5/xliguide.5.gz

# The defaults file is only 2 lines, easier to create here than
# try to make the Makefile play nice with DESTDIR. Plus, the hard-coded
# defaults need modifying anyway (add formats to extension= and use dirs
# that actually exist for path=).
mkdir -p $PKG/etc/X11/app-defaults
cat <<EOF >$PKG/etc/X11/app-defaults/Xli
path=/usr/share/pixmaps /usr/share/icons
extension=.png .gif .jpg .PNG .GIF .JPG .jpeg .JPEG .xpm .pcx .PCX .bmp .BMP .pbm .pgm .ppm .tga .TGA .rle .csun .msun .sun .face .xbm .bm
EOF

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a ABOUTGAMMA CHANGES README* TODO chkgamma.jpg $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
